package com.virus.mall.controller.admin;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.virus.mall.model.Result;
import com.virus.mall.model.bo.ChangeOrderBo;
import com.virus.mall.model.bo.OrdersByPageBo;
import com.virus.mall.model.vo.OrderVo;
import com.virus.mall.model.vo.OrdersByPageVo;
import com.virus.mall.model.vo.OrdersVo;
import com.virus.mall.service.OrdersService;
import com.virus.mall.service.impl.OrdersServiceImpl;
import com.virus.mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Classname OrderServlet
 * @Description NULL
 * @Date 2021/4/19 14:02
 * @Created by virus
 */
@WebServlet("/api/admin/order/*")
public class OrdersServlet extends HttpServlet {

    private ObjectMapper objectMapper = new ObjectMapper();

    private OrdersService ordersService = new OrdersServiceImpl();

    {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replaceAll("/api/admin/order/", "");
        if (op.equals("ordersByPage")) {
            ordersByPage(request, response);
        } else if (op.equals("changeOrder")) {
            changeOrder(request, response);
        }
    }

    /**
     * 根据页面获取订单信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void ordersByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        OrdersByPageBo ordersByPageBo = null;
        try {
            ordersByPageBo = objectMapper.readValue(requestBody, OrdersByPageBo.class);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("参数类型不正确")));
            return;
        }
        OrdersByPageVo ordersByPageVo = ordersService.orderByPage(ordersByPageBo);
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(ordersByPageVo)));
    }

    /**
     * 改变订单信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void changeOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        ChangeOrderBo changeOrderBo = null;
        try {
            changeOrderBo = objectMapper.readValue(requestBody, ChangeOrderBo.class);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("参数类型错误")));
            return;
        }
        boolean result = ordersService.changeOrder(changeOrderBo);
        if (result) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.ok()));
        } else {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("改变订单信息失败")));
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String op = requestURI.replaceAll("/api/admin/order/", "");
        if (op.startsWith("orders")) {
            orders(request, response);
        } else if (op.startsWith("order")) {
            order(request, response);
        } else if (op.startsWith("deleteOrder")) {
            deleteOrder(request, response);
        }
    }

    /**
     * 根据状态获取订单信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void orders(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String stateStr = request.getParameter("state");
        int state;
        try {
            state = Integer.parseInt(stateStr);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("参数类型错误")));
            return;
        }
        List<OrdersVo> ordersVos =  ordersService.orders(state);
        String s = objectMapper.writeValueAsString(Result.ok(ordersVos));
        System.out.println(s);
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(ordersVos)));
    }

    /**
     * 根据id获取订单信息
     * @param request
     * @param response
     * @throws IOException
     */
    private void order(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        OrderVo orderVo = null;
        try {
            orderVo = ordersService.order(Integer.parseInt(id));
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("查询订单失败")));
            return;
        }
        if (orderVo == null) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("订单信息为空")));
            return;
        }
        response.getWriter().println(objectMapper.writeValueAsString(Result.ok(orderVo)));
    }

    /**
     * 删除订单
     * @param request
     * @param response
     * @throws IOException
     */
    private void deleteOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String idStr = request.getParameter("id");
        int id;
        try {
            id = Integer.parseInt(idStr);
        } catch (Exception e) {
            e.printStackTrace();
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("参数类型错误")));
            return;
        }
        boolean result = ordersService.deleteOrder(id);
        if (result) {
            response.getWriter().println(objectMapper.writeValueAsString(Result.ok()));
        } else {
            response.getWriter().println(objectMapper.writeValueAsString(Result.error("删除订单失败")));
        }
    }
}
